package com.goalias.controller;


import com.goalias.domain.dto.ArticleDTO;
import com.goalias.domain.dto.ArticleQueryDTO;
import com.goalias.domain.entity.Article;
import com.goalias.exception.ExceptionEnum;
import com.goalias.resp.Result;
import com.goalias.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 文章表 前端控制器
 * </p>
 *
 * @author goalias
 * @since 2024-12-12
 */
@RestController
@RequestMapping("/content/article")
public class ArticleController {

    @Resource
    private IArticleService articleService;

    @GetMapping("/hotList")
    public Result getHotArticleList() {
        return articleService.getHotArticleList();
    }

    @GetMapping("/getList")
    public Result getArticleList(@Validated ArticleQueryDTO articleQueryDTO){
        return articleService.getArticleList(articleQueryDTO);
    }

    @GetMapping("/{id}")
    public Result getArticleDetail(@PathVariable("id") Long id) {
        return articleService.getArticleDetail(id);
    }

    @PostMapping("/add")
    public Result addArticle(@Validated @RequestBody ArticleDTO article){
        return articleService.addArticle(article);
    }

    @PostMapping("/edit")
    public Result editArticle(@Validated @RequestBody ArticleDTO article){
        return articleService.editArticle(article);
    }

    @PostMapping("/delete/{id}")
    public Result editArticle(@PathVariable Long id){
        Article article = new Article();
        article.setId(id);
        article.setDelFlag(1);
        boolean success = articleService.updateById(article);
        if (!success){
            return Result.error(ExceptionEnum.DATABASE_ERROR);
        }
        return Result.success();
    }

    @GetMapping("/count")
    public Result getArticleCount(){
        return articleService.getArticleCount();
    }

    @PostMapping("/updateViewCount/{id}")
    public Result updateViewCount(@PathVariable Long id){
        return articleService.updateViewCount(id);
    }

    @GetMapping("/previousNext/{id}")
    public Result getPreviousNextArticle(@PathVariable Long id){
        return articleService.getPreviousNextArticle(id);
    }


}
